package com.auxgroup.hr.front.service.process.impl;

import com.auxgroup.hr.front.commons.enums.WfStatusEnum;
import com.auxgroup.hr.front.commons.enums.WfTypeEnum;
import com.auxgroup.hr.front.commons.utils.ObjectFieldUtils;
import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.dao.domain.AbsWfEmplDomain;
import com.auxgroup.hr.front.dao.domain.JobCurrPrimary;
import com.auxgroup.hr.front.dao.domain.PsCJobCurrVw;
import com.auxgroup.hr.front.dao.domain.PsCWfEmpVw;
import com.auxgroup.hr.front.dao.repository.PsCJobCurrVwRepository;
import com.auxgroup.hr.front.dao.repository.PsCWfEmpVwRepository;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;

/**
 * Created by nitongqian on 2019/3/3.
 */
@Service("wfInitService")
public class WfInitServiceImpl {

    @Autowired
    private PsCWfEmpVwRepository psCWfEmpVwRepository;

    @Autowired
    private PsCJobCurrVwRepository psCJobCurrVwRepository;

    @Resource(name = "dozerMapper")
    protected IObjectMapper objectMapper;
    
    public<T extends AbsWfEmplDomain>  T init(T wkDomain, UserDTO user, String transNum) {


        wkDomain.setcTransactionNbr(transNum);
        wkDomain.setcWfStatus(WfStatusEnum.WF_STATUS_S.getStatus()); // 流程状态
        wkDomain.setcWfAppDt(new Date());
        //wkDomain.setText254();
        //wkDomain.setcOtherExplain();

        wkDomain.setCreatedBy(user.getOprId()); // 创建人用户
        wkDomain.setCreatedttm(new Timestamp(System.currentTimeMillis())); // 创建时间
        wkDomain.setLastupdoprid(user.getOprId()); // 更新者
        wkDomain.setLastupddttm(new Timestamp(System.currentTimeMillis())); // 更新时间

        //发起人信息填充
        JobCurrPrimary primary = new JobCurrPrimary();
        primary.setEmplid(user.getEmplId());
        primary.setEmplRcd(new BigDecimal(0));
        PsCWfEmpVw emp = psCWfEmpVwRepository.findOne(primary);//获取个人信息内容

        wkDomain.setcWfEmplid(emp.getEmplid());
        wkDomain.setcWfEmplRcd(emp.getEmplRcd());
        wkDomain.setcWfName(emp.getNameDisplay());
        wkDomain.setcWfBu(emp.getBusinessUnit());
        //wkDomain.setcWfSetidDept(emp.getse);
//        wkDomain.setcWfDeptid(emp.getDeptid());
        wkDomain.setcWfDeptdescr(emp.getDeptDescr());
        wkDomain.setcWfJobcode(emp.getJobcode());
        wkDomain.setcWfJcDescr(emp.getJobcodeDescr());
        wkDomain.setcWfPhone(emp.getPhone());
        wkDomain.setcJobType2(emp.getcJobType());
        wkDomain.setcWfJobLevel(emp.getcJobLevel());
        wkDomain.setcWklifBgnDt2(emp.getLastHireDt());

        //wkDomain.setcEmplid2();
        //wkDomain.setcJobType2();
        //wkDomain.setcWklifBgnDt2();
        PsCJobCurrVw cJobCurrVw = psCJobCurrVwRepository.findOne(new JobCurrPrimary(BigDecimal.ZERO, user.getEmplId()));
        if(cJobCurrVw != null) {
            wkDomain.setcWfSetidDept(cJobCurrVw.getSetidDept());
            wkDomain.setcWfDeptid(cJobCurrVw.getDeptid());
        }


        return wkDomain;
    }
}
